Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: query max amountIn based on max ticks willing to traverse #5889

Merged
merged 8 commits into from
Jul 28, 2023

Conversation

czarcas7ic
Copy link
Member

@czarcas7ic czarcas7ic commented Jul 25, 2023

Closes: #5884

What is the purpose of the change

This PR provides a new API (ComputeMaxInAmtGivenMaxTicksCrossed), which is used to determine the max amount a token cab be swapped in within a CL pool given a max number of ticks that can be traversed.

Testing and Verifying

TestComputeMaxInAmtGivenMaxTicksCrossed added.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes?
  • Changelog entry added to Unreleased section of CHANGELOG.md?

Where is the change documented?

  • Specification (x/{module}/README.md)
  • Osmosis documentation site
  • Code comments?
  • N/A

Comment on lines +275 to +277
if err != nil {
return nil, err
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive by lint (unchecked error)

@@ -808,3 +808,120 @@ func edgeCaseInequalityBasedOnSwapStrategy(isZeroForOne bool, nextInitializedTic
}
return nextInitializedTickSqrtPrice.LT(computedSqrtPrice)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer, ComputeMaxInAmtGivenMaxTicksCrossed follows nearly the same logic as calcIn/Out, but tracks the ticks traversed, does not track the spread or uptime accums, and does not save changes to state.

@czarcas7ic czarcas7ic marked this pull request as ready for review July 26, 2023 00:10
@czarcas7ic czarcas7ic requested a review from p0mvn as a code owner July 26, 2023 00:10
@NotJeremyLiu
Copy link
Contributor

This interface looks great to me and is all that Protorev will need to bound properly, thanks for this!

ctx sdk.Context,
poolId uint64,
tokenInDenom string,
maxTicksCrossed uint64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: since tick liquidity is subject to change, how is protorev going to bound the max tick a swap is going to cross? Is it something like X swap can cross upto 100 ticks given the gas to cross that 100 ticks is <50M

I originally thought this function was going to be given X amt estimate how many ticks it will cross, but if this works then it's all g.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stackman27 I previously thought we wanted "Given X amount, how many ticks will it cross?" but since Protorev needs to bound itself from consuming too much gas and compute time, and it seems to me liquidity doesn't really impact that, but number of ticks crossed does, so it cares more about the ticks crossed.

If a method was made that given X amount how many ticks will it cross, and we put in $1k to swap in and it says it'll cross 1000 ticks, that would be too much and then the next action would be "okay how do I reduce the number of ticks crossed to something acceptable", so it feels like just querying directly for the acceptable number of ticks crossed and always using that amountIn bound is better than continuously trying in different amounts to identify what amount passes an acceptable amount of ticks.

Copy link
Contributor

@stackman27 stackman27 Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see one last question, so in the current implementation how are you going to decide the maxTickCrossed value? are you going to predetermine that 50M or 100M gas can cross X amt of ticks and generate routes for swap?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the answer to this is crossing ticks should cost a fixed amt of gas so yes, your description should be accurate (but admittedly I just made this based of Skip's design requirements and treated the actual use of the API as a black box)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stackman27 @czarcas7ic Yes exactly. The plan is to make X number of ticks be a changeable variable (similar to pool points), and change that depending on how many CL pools we want to be able to use in backrunning.

That is, if many new CL pools launch and almost all swaps are on CL pools, then we'd want to decrease the max number of ticks crossed per pool so that we can check 2-4 routes per swap without running out of the upperGasLimit, if there are still more balancer swaps than CL, and our routes mainly consist of balancer pool, then we can increase X to cross more ticks per CL pool since there are less of them, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, would love to review the PR when its ready too

}

// Initialize swap state
swapState := newSwapState(types.MaxSpotPrice.RoundInt(), p, swapStrategy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use types.MaxSpotPrice? Do we decrement it until maxTicksCrossed ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we basically never want the reason we stop an estimation to be due to a lack of funds, it should only ever strictly be due to the limit imposed by amt of ticks willing to traverse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in that case would TotalPoolLiquidity not be a good value to use?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that makes sense too, I can change it to that

Copy link
Contributor

@stackman27 stackman27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this LGTM. I'm just onboarding on swap logic so would love a second pair of eye here before merge

@czarcas7ic
Copy link
Member Author

@NotJeremyLiu Am merging this now, just pinging for awareness

@czarcas7ic czarcas7ic merged commit 7523559 into main Jul 28, 2023
@czarcas7ic czarcas7ic deleted the adam/protorev-cl-amount-in-tick-filter-query branch July 28, 2023 20:02
VitalyV1337 pushed a commit to VitalyV1337/osmosis-1 that referenced this pull request Jul 31, 2023
…sis-labs#5889)

* max amountIn based on max ticks willing to trav

* update changelog

* clean up

* use cache context in the query

* change amount in from max spot price to pool bal

* change to tokenoutdenom
@github-actions github-actions bot mentioned this pull request Mar 15, 2024
@github-actions github-actions bot mentioned this pull request May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Protorev Binary Search Range Bounding For Concentrated Liquidity Pools
3 participants